#include "nonplayablecharacter.h"
#include <stdlib.h>


    NonPlayableCharacter::NonPlayableCharacter(int x, int y, int estado, GLuint textura, double forca, double raio, int nextWaypoint)
    {
        //this antes de tudo
        setx(x);
        sety(y);
        setEstado(estado);
        setTextura(textura);
        setForca(forca);
        setRaio(raio);
        setNextWaypoint(nextWaypoint);
        //fim this antes de tudo
    }

    NonPlayableCharacter::NonPlayableCharacter(double xCont, double yCont, int estado, GLuint textura, double forca, double raio, int nextWaypoint)
    {
        //this antes de tudo
        setxCont(xCont);
        setyCont(yCont);
        setEstado(estado);
        setTextura(textura);
        setForca(forca);
        setRaio(raio);
        setNextWaypoint(nextWaypoint);
        //fim this antes de tudo
    }

    void NonPlayableCharacter::setNextWaypoint(int way){
        this->nextWaypoint = way;
    }
    int NonPlayableCharacter::getNextWaypoint(){
        return this->nextWaypoint;
    }

    void NonPlayableCharacter::waypoint_nav(int x, int y, int next){
        if ( this->getx() == x && this->gety() == y ){
            this->setNextWaypoint( next );
        }
        else{
            if( this->getx() < x){
                this->setx( this->getx() + 1);
            }
            else if( this->getx() > x){
                this->setx( this->getx() - 1);
            }
            if( this->gety() < y){
                this->sety( this->gety() + 1);
            }
            else if( this->gety() > y){
                this->sety( this->gety() - 1);
            }
        }
    }

    bool NonPlayableCharacter::avistou(int x , int y ){
        if( abs(this->getx() - x) < 9 && abs(this->gety() - y) < 9 ){
            return true;
        }
        else{
            return false;
        }
    }

    void NonPlayableCharacter::perseguicao(int x, int y){
        if ( this->getx() == x && this->gety() == y ){
            return;
        }
        else{
            if( this->getx() < x){
                this->setx( this->getx() + 1);
            }
            else if( this->getx() > x){
                this->setx( this->getx() - 1);
            }
            if( this->gety() < y){
                this->sety( this->gety() + 1);
            }
            else if( this->gety() > y){
                this->sety( this->gety() - 1);
            }
        }
    }

    void NonPlayableCharacter::campos_potenciais(int **grid){
        int menorVizinho;
        int x,y;
        int destinoX,destinoY;

        x = this->getx();
        y = this->gety();
        menorVizinho = grid[x][y];
        destinoX = x;
        destinoY = y;
        if(menorVizinho!=0){
            if( y > 0){
                if( grid[x][y-1] < menorVizinho ){
                    destinoX = x;
                    destinoY = y-1;
                    menorVizinho = grid[x][y-1];
                }
            }
            if( x <= TAMANHO_GRID-2){
                if( grid[x+1][y] < menorVizinho ){
                    destinoX = x+1;
                    destinoY = y;
                    menorVizinho = grid[x+1][y];
                }
            }
            if( x > 0){
                if( grid[x-1][y] < menorVizinho ){
                    destinoX = x-1;
                    destinoY = y;
                    menorVizinho = grid[x-1][y];
                }
            }
            if( y <= TAMANHO_GRID-2){
                if( grid[x][y+1] < menorVizinho ){
                    destinoX = x;
                    destinoY = y+1;
                    menorVizinho = grid[x][y+1];
                }
            }
            if( (x > 0) && (y > 0) ){
                if( grid[x-1][y-1] < menorVizinho ){
                    destinoX = x-1;
                    destinoY = y-1;
                    menorVizinho = grid[x-1][y-1];
                }
            }

            if( (x > 0) && (y <= TAMANHO_GRID-2) ){
                if( grid[x-1][y+1] < menorVizinho ){
                    destinoX = x-1;
                    destinoY = y+1;
                    menorVizinho = grid[x-1][y+1];
                }
            }

            if( (x <= 99) && (y <= TAMANHO_GRID-2) ){
                if( grid[x+1][y+1] < menorVizinho ){
                    destinoX = x+1;
                    destinoY = y+1;
                    menorVizinho = grid[x+1][y+1];
                }
            }
            if( (x <= TAMANHO_GRID-2) && (y > 0) ){
                 if( grid[x+1][y-1] < menorVizinho ){
                    destinoX = x+1;
                    destinoY = y-1;
                    menorVizinho = grid[x+1][y-1];
                }
            }
            if(this->getx() == destinoX  &&  this->gety() == destinoY){
                 if( grid[destinoX][destinoY]!=0 )grid[destinoX][destinoY] = grid[destinoX][destinoY]+100;
            }
            this->setx(destinoX);
            this->sety(destinoY);
        }
    }
